﻿using ZYCommon;
using System;
using System.Xml;
using TextEditor.TextDocument.Document;

namespace TextEditor
{
    

    public class ZYVBScriptElement : IXMLSerializable
    {
        private bool bolEditPWDChecked = false;
        private bool bolScriptModified = false;
        private ZYTextDocument myOwnerDocument = null;
        private string strEditPWD = null;
        private string strRunPWD = null;
        private string strSourceCode = null;
        private string strSrc = null;

        public void AppendSourceCode(string strCode)
        {
            this.strSourceCode = this.strSourceCode + "\r\n" + strCode;
            this.bolScriptModified = true;
        }

        public bool CheckEditPWD(string strPWD)
        {
            this.bolEditPWDChecked = StringCommon.isBlankString(this.strEditPWD) || (StringCommon.GetMD5String(strPWD) == this.strEditPWD);
            return this.bolEditPWDChecked;
        }

        public bool CheckRunPWD(string strPWD)
        {
            return (StringCommon.isBlankString(this.strRunPWD) || (StringCommon.GetMD5String(strPWD) == this.strRunPWD));
        }

        public void Clear()
        {
            this.strRunPWD = null;
            this.strEditPWD = null;
            this.strSourceCode = null;
            this.strSrc = null;
            this.bolEditPWDChecked = true;
        }

        public string ConvertToRunTimeScript(string strText)
        {
            if (StringCommon.isBlankString(this.strRunPWD))
            {
                if (this.myOwnerDocument != null)
                {
                    strText = this.myOwnerDocument.Variables.FixVariableString(strText);
                }
                return strText;
            }
            return null;
        }

        public bool FromXML(XmlElement RootElement)
        {
            this.Clear();
            if (RootElement != null)
            {
                this.strRunPWD = RootElement.GetAttribute("runpwd");
                this.strEditPWD = RootElement.GetAttribute("editpwd");
                this.strSrc = RootElement.GetAttribute("src");
                this.bolEditPWDChecked = StringCommon.isBlankString(this.strEditPWD);
                if (StringCommon.HasContent(this.strSrc))
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(this.strSrc);
                    return this.FromXML(document.DocumentElement);
                }
                this.strSourceCode = RootElement.InnerText;
                return true;
            }
            return false;
        }

        public string GetXMLName()
        {
            return "script";
        }

        public bool HasEditPWD()
        {
            return StringCommon.HasContent(this.strEditPWD);
        }

        public bool HasRunPWD()
        {
            return StringCommon.HasContent(this.strRunPWD);
        }

        public bool ToXML(XmlElement RootElement)
        {
            if (RootElement != null)
            {
                RootElement.RemoveAll();
                RootElement.SetAttribute("runpwd", this.strRunPWD);
                RootElement.SetAttribute("editpwd", this.strEditPWD);
                if (StringCommon.HasContent(this.strSrc))
                {
                    RootElement.SetAttribute("src", this.strSrc);
                }
                else
                {
                    RootElement.AppendChild(RootElement.OwnerDocument.CreateCDataSection(this.strSourceCode));
                }
                return true;
            }
            return false;
        }

        public string EditPWD
        {
            get
            {
                return this.strEditPWD;
            }
            set
            {
                if (StringCommon.isBlankString(value))
                {
                    this.strEditPWD = null;
                }
                else
                {
                    this.strEditPWD = StringCommon.GetMD5String(value.Trim());
                }
            }
        }

        public bool EditPWDChecked
        {
            get
            {
                return this.bolEditPWDChecked;
            }
        }

        public ZYTextDocument OwnerDocument
        {
            get
            {
                return this.myOwnerDocument;
            }
            set
            {
                this.myOwnerDocument = value;
            }
        }

        public string RunPWD
        {
            get
            {
                return this.strRunPWD;
            }
            set
            {
                if (StringCommon.isBlankString(value))
                {
                    this.strRunPWD = null;
                }
                else
                {
                    this.strRunPWD = StringCommon.GetMD5String(value.Trim());
                }
            }
        }

        internal string RunTimeSourceCode
        {
            get
            {
                return this.ConvertToRunTimeScript(this.strSourceCode);
            }
        }

        public bool ScriptModified
        {
            get
            {
                return this.bolScriptModified;
            }
            set
            {
                this.bolScriptModified = value;
            }
        }

        public string SourceCode
        {
            get
            {
                if (this.bolEditPWDChecked)
                {
                    return this.strSourceCode;
                }
                return null;
            }
            set
            {
                this.strSourceCode = value;
                this.bolScriptModified = true;
            }
        }

        public string Src
        {
            get
            {
                return this.strSrc;
            }
            set
            {
                this.strSrc = value;
            }
        }
    }
}

